module.exports = function ($scope, $resource, $modal, toaster) {

	//查询法规列表
	$scope.search = function() {
		$resource('/api/as/atc/policyrule/getPolicyRuleList', {}, {}).save({}, function(res){
            if (res.errcode === 0) {
                $scope.policyRuleList = res.data;
            } else {
                toaster.error({title: '', body: '查询政策及法规信息失败'});
            }
        });
	}

	// $scope.searchform = {};
	$scope.search();


    //修改发布状态
    $scope.setRuleStatus = function(item) {
        var msg = item.status == '1' ? '取消本条补贴办法的展示吗?' : '确认发布本条补贴办法吗?';
        if(confirm(msg)){
			var para = {
				'code': item.code,
				'status': item.status == '0' ? '1' : '0'
			}
			$resource('/api/as/atc/policyrule/updatePolicyRule', {}, {}).save(para, function(res){
				if (res.errcode === 0) {
					toaster.success({title: '', body: '成功'});
					$scope.search();
				} else {
					toaster.error({title: '', body: res.errmsg});
				}
			});
        }
	};
	
	//添加
	$scope.addPolicyRule = function() {
		var modalInstance = $modal.open({
			template: require('../views/policyRuleInfo.html'),
			controller: 'policyRuleInfo',
			size: 'lg',
			resolve: {
				item: function () {
					return null;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			$scope.search();
		});
	}

    //修改
	$scope.edit = function(item) {
		var modalInstance = $modal.open({
			template: require('../views/policyRuleInfo.html'),
			controller: 'policyRuleInfo',
			size: 'lg',
			resolve: {
				item: function () {
					return item;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			item.title = result.title;
			item.context = result.context;
		});
    };

	//删除
	$scope.delete = function(code, index) {
		if(confirm('确认删除本条补贴办法吗?')){
			$resource('/api/as/atc/policyrule/deletePolicyRule', {}, {}).save({'code': code}, function(res){
				if (res.errcode === 0) {
					toaster.success({title: '', body: '删除成功'});
					$scope.policyRuleList.splice(index, 1);
				} else {
					toaster.error({title: '', body: '删除失败,请稍后再试'});
				}
			});
		}
	}

};